import JSEncrypt from 'jsencrypt';
// import NodeRsa from 'node-rsa'
import NodeRSA from 'node-rsa';


const privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAl5HtaZ3Ww5tGFY+Zr0CbEWJNN2rCim9Hg+hwbIFimPkk/2ZMfl6E7X/QYfYomWqGztAd2xOWC3vFFWH9ddD/1QIDAQABAkBdkG+GKzFQPqHc3XB5huAyswQHmZfN7TOwceQ0lwpOc0r41AHm6JIRVdahjs3LMJ+mE5E9pWQuWcrOb++k8zwBAiEAxSlz2blFQ8dcj2+PIe4K+tRsEUvZyDW/rIyoV7Fmf2ECIQDEzWXz1wDmEPpZwYRXP5tqbF+MRfutnXR1BUpw78gY9QIgI4a5olS4/2KP8iSW9PUws0vNgTxarLHKrOvXdZ24q2ECIB+e6Pyu1QQiQjddsofaPnxxAUEmlXOl0upieT2AKWRpAiEAkxieTnxH8Kwna0P0Q4U5zJgRl8AxzVmgDox98RWYtP8=";
const publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJeR7Wmd1sObRhWPma9AmxFiTTdqwopvR4PocGyBYpj5JP9mTH5ehO1/0GH2KJlqhs7QHdsTlgt7xRVh/XXQ/9UCAwEAAQ==";


// 加密
export const des_encrypt = (data, publicKey) => {
    // publicKey = publicKey;
    let rsa = new JSEncrypt() // 新建JSEncrypt对象
    rsa.setPublicKey(publicKey) // 设置公钥
    console.log(publicKey)
    // 对需要加密的数据进行加密
    // return encryptor.encrypt(data)

    // var data = "dddddwwwwww13ssssddssswwwwweedsjidsfiosgfohgfiodhghdfghbijfdbgdfbgbdfjgbdsbgidsbgsdbgisdbgiu";
    var data = "ddddd";
    var chunkSize = rsa.getKey().n.bitLength() / 8 - 11;
    console.log("size:", chunkSize)
    var encryptedData = "";
    for (var i = 0; i < data.length; i += chunkSize) {
        var chunk = data.substr(i, chunkSize);
        encryptedData += rsa.encrypt(chunk);
    }
    return encryptedData;
}
// 解密
export const des_decrypt = (data) => {
    let rsa = new JSEncrypt();
    // 设置私钥
    rsa.setPrivateKey(privateKey);
    var res = "";
    let chunkSize = 88
    for (var i = 0; i < data.length; i += chunkSize) {
        var chunk = data.substr(i, chunkSize);
        res += rsa.decrypt(chunk);
    }
    return res;
}


let data = {
    "d": "1111",
    "c": 'sddddd',
    "name": "huhuhuhuhuhuhuh",
    'age': 133333333333,
    "no": "3drjjbjhjhjdfgseefgwefeeeeeefdfdffddgsgddggdfdggdgdgdsgddggdgdsgsgsgs"
}

let str = des_encrypt(data.toString(), publicKey);
console.log("sign:", str)
let s = des_decrypt(str);
console.log("解密:", s)


// var NodeRSA = require('node-rsa');


let key = new NodeRSA({b: 512}); //生成新的512位长度密
let text = 'Hello RSA!'; // 加密前数据
// key.setOptions({ encryptionScheme: 'pkcs1' });
let encrypted = key.encrypt(data, "base64");  // 加密后数据
console.log("encrypted:", encrypted);
let decrypted = key.decrypt(encrypted, 'utf8'); // 解密后数据
console.log('decrypted: ', decrypted);







